package com.bawei.mall.product.controller;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.mall.product.service.ICAddressService;
import com.bawei.mall.product.utils.AnalyzeAddres;
import com.bawei.mall.product.domain.Po.Address;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：liurui
 * @Package：com.bawei.mall.search.controller
 * @Project：mall_cloud
 * @name：UserProductController
 */
@RestController
@Log4j2
@RequestMapping("/address")
public class CAddressController {
    @Autowired
    private ICAddressService service;

    @Autowired
    private AnalyzeAddres analyzeAddres;

    private static final Integer SETID=1;
    private static final Integer SETMEMBERID=11;
    @GetMapping("/list")
    public R<List<Address>> detail(){
        List<Address> list=service.findAddress();
        return R.ok(list);
    }


    @PostMapping("/add")
    public R add(@RequestBody Address address){
        service.add(address);
        return R.ok();
    }

    @GetMapping("/findaddress/{id}")
    public R<Address> findaddress(@PathVariable Integer id){
        Address address=service.findaddress(id);
        return R.ok(address);
    }

    @PostMapping("/update/{id}")
    public R update(@PathVariable Integer id,@RequestBody Address address){
        service.update(address,id);
        return R.ok();
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        service.delete(id);
        return R.ok();
    }

    @PostMapping("/address/{zdsbaddress}")
    public R address(@PathVariable String zdsbaddress) {
        try {
            String s = analyzeAddres.analyzeAddres(zdsbaddress);
            System.out.println("地址" + s);

            JSONObject jsonObject = JSONObject.parseObject(s);
            JSONObject data = jsonObject.getJSONObject("data");

            // 使用Optional或检查字段是否存在
            String province = data.getString("province");
            String city = data.getString("city");
            String county = data.getString("county");
            String town = data.getString("town");
            String detail = data.getString("detail");

            // 注意：这里假设phoneNum和person字段总是存在的
            String phoneNum = data.getString("phoneNum");
            String person = data.getString("person");

            // 注意：这里假设邮编是作为一个单独的字段，而不是作为data对象的一个键
            // 如果邮编是作为"518000"这样的键存在的，你需要修改这个逻辑
            String postCode = data.getString("postCode"); // 假设postCode是JSON中的一个字段

            // 假设id和memberId是从请求或其他业务逻辑中获取的
            // int id = ...; // 从请求或其他地方获取
            // int memberId = ...; // 从请求或其他地方获取

            // 使用一个方法来创建和返回Address对象
            Address address = createAddress(person, phoneNum, postCode, province, city, county, town, detail);

            // 假设R.ok是一个通用的方法来创建一个成功的响应
            return R.ok(address);
        } catch (Exception e) {
            // 处理异常，例如返回错误响应
            e.printStackTrace();
            return R.fail("解析地址时发生错误");
        }
    }

    private Address createAddress(String person, String phoneNum, String postCode, String province, String city, String county, String town, String detail) {
        Address address = new Address();
        // 假设这里有一个逻辑来设置id和memberId
        address.setId(SETID);
        address.setMemberId(SETMEMBERID);
        address.setName(person);
        address.setPhoneNumber(phoneNum);
        address.setPostCode(postCode);
        address.setProvince(province);
        address.setCity(city);
        address.setRegion(county); // 通常region指的是城市下的区域，如区、县，但这里根据你的代码，我假设county是区域
        address.setDetailAddress(town + detail);
        return address;
    }
}
